package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Client;
import cn.wolfcode.crm.query.ClientChartQueryObject;
import cn.wolfcode.crm.service.IChartService;
import cn.wolfcode.crm.service.IProChartService;
import cn.wolfcode.crm.util.JSONUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RequestMapping("proEchart")
@Controller
public class ProEchartController {
    @Autowired
    private IProChartService service;

//    @RequestMapping("view")
//    public String view() throws Exception {
//        return "proEchart";
//    }
    //由于dataGrid组件返回必须为json,所以跳转界面就分开

    //页面数据
    @RequestMapping("list")
    @ResponseBody
    public List<Map<String, Object>> list(ClientChartQueryObject qo) {
        return service.proChart(qo);
    }

    //会员柱状图
    @RequestMapping("view")
    public String saleByBar(@ModelAttribute("qo") ClientChartQueryObject qo, Model model){
        List<Object> x = new ArrayList<>();
        List<Object> y = new ArrayList<>();
        //先获取集合里的数据
        List<Map<String,Object>> maps = service.proChart(qo);
        //然后把集合迭代出来得到map
        for (Map<String, Object> map : maps) {
            //取出当前行中的name和销售金额两个值存入x,y集合中
            x.add(map.get("name"));
            y.add(map.get("number"));
        }
        //转换成json,传递参数
        model.addAttribute("x", JSONUtil.toJSONString(x));
        model.addAttribute("y",JSONUtil.toJSONString(y));
        return "proEchart";
    }

    //查询销售笔数
    @RequestMapping("selectByNumber")
    @ResponseBody
    public Integer selectByNumber(Client num) {
        try {
            return service.selectByNumber(num);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //查询销售总额
    @RequestMapping("totalPrice")
    @ResponseBody
    public Integer totalPrice(Client maney){
        try {
            return service.totalPrice(maney);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //查询消售毛利
    @RequestMapping("profit")
    @ResponseBody
    public Integer profit(Client name){
        try {
            return service.profit(name);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
